package com.corpgovernment.organization.controller.paytype;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.corpgovernment.api.organization.model.paytype.*;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.enums.PayTypeEnum;
import com.corpgovernment.common.enums.SiteEnum;
import com.corpgovernment.organization.entity.db.MbOrgPayType;
import com.corpgovernment.organization.service.IOrgPayTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 支付方式接口
 *
 * @author wangyujue
 */
@RestController
@RequestMapping("/payType")
public class OrgPayTypeController {

    @Autowired
    private IOrgPayTypeService orgPayTypeService;

    /**
     * 服务商支付范围
     *
     * @param payRangeRequest
     * @return
     */
    @RequestMapping("/payRange")
    JSONResult<PayRangeResponse> payRange(@RequestBody PayRangeRequest payRangeRequest) {
        PayRangeResponse payRangeResponse = new PayRangeResponse();
        String orgId = payRangeRequest.getOrgId();
        // 机火支付方式
        List<MbOrgPayType> mbOrgPayTypeList = orgPayTypeService.findByOrgId(orgId, SiteEnum.FLIGHT.getCode());
        if (CollUtil.isNotEmpty(mbOrgPayTypeList)) {
            MbOrgPayType mbOrgPayType = mbOrgPayTypeList.get(0);
            payRangeResponse.setRangeFlag(mbOrgPayType == null || mbOrgPayType.getPaytype().equals(PayTypeEnum.PPAY.getType()));
        }
        // 获取酒店支付方式
        List<MbOrgPayType> hotelPayTypeList = orgPayTypeService.findByOrgId(orgId, SiteEnum.HOTEL.getCode());
        if (CollUtil.isNotEmpty(hotelPayTypeList)) {
            List<MbOrgPayType> accntList = hotelPayTypeList.stream().filter(p -> StrUtil.equals(p.getPaytype(), PayTypeEnum.ACCNT.getType())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(accntList)) {
                payRangeResponse.setHotelRange(false);
            } else {
                payRangeResponse.setHotelRange(true);
            }
        } else {
            payRangeResponse.setHotelRange(true);
        }
        return new JSONResult<>(payRangeResponse);
    }

    /**
     * 获取用户支付方式
     * @param request
     * @return
     */
    @RequestMapping(value = "/listHotelPayType")
    public JSONResult<ListHotelPayTypeResponse> listHotelPayType(@RequestBody ListHotelPayTypeRequest request) {
        ListHotelPayTypeResponse listHotelPayTypeResponse = new ListHotelPayTypeResponse();
        String orgId = request.getOrgId();
        SiteEnum site = request.getSite();
        String supplierCode = request.getSupplierCode();
        List<MbOrgPayType> mbOrgPayTypeList = orgPayTypeService.listByOrgIds(CollUtil.newArrayList(orgId), site.getCode(), null, null);
        if (CollUtil.isEmpty(mbOrgPayTypeList)) {
            return new JSONResult<>(listHotelPayTypeResponse);
        }
        mbOrgPayTypeList = mbOrgPayTypeList.stream().filter(p -> StrUtil.isBlank(p.getSuppliercode()) || StrUtil.equals(p.getSuppliercode(), supplierCode)).collect(Collectors.toList());
        List<OrgPayTypeVo> returnList = new ArrayList<>();
        for (MbOrgPayType mbOrgPayType : mbOrgPayTypeList) {
            OrgPayTypeVo orgPayTypeVo = new OrgPayTypeVo();
            orgPayTypeVo.setOrgId(mbOrgPayType.getOrgId());
            orgPayTypeVo.setPaytype(mbOrgPayType.getPaytype());
            orgPayTypeVo.setSite(mbOrgPayType.getSite());
            orgPayTypeVo.setSuppliercode(mbOrgPayType.getSuppliercode());
            returnList.add(orgPayTypeVo);
        }
        listHotelPayTypeResponse.setPayTypeList(returnList);
        return new JSONResult<>(listHotelPayTypeResponse);
    }
}
